package com.yjyw.tiexie.view;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.yjyw.tiexie.R;

/**
 * Created on 2019/7/22.
 *
 * @author ${AUTHOR}
 * TODO:
 */
public class MyProgressDialog extends AlertDialog {

    private TextView mMessageView;
    private CharSequence mMessage;

    public static MyProgressDialog show(Context context, CharSequence title, CharSequence message) {
        return show(context, title, message, false);
    }

    public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean cancelable) {
        return show(context, title, message, cancelable, null);
    }

    public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean cancelable,
                                        OnCancelListener cancelListener) {
        MyProgressDialog dialog = new MyProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;
    }

    public MyProgressDialog(Context context) {
        super(context, R.style.CustomProgressDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_progress, null);
        mMessageView = (TextView) view.findViewById(android.R.id.message);
        setView(view);
        if (mMessage != null) {
            if ("".equals(mMessage)) {
                mMessageView.setVisibility(View.GONE);
            } else {
                mMessageView.setVisibility(View.VISIBLE);
            }
            setMessage(mMessage);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setMessage(CharSequence message) {
        if (mMessageView != null) {
            mMessageView.setText(message);
        } else {
            mMessage = message;
        }
    }
}
